package control;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import model.AreaAtuacao;
import business.AreaAtuacaoDAO;

@ManagedBean(name="areaAtuacaoHandler")
@SessionScoped

public class AreaAtuacaoHandler implements Serializable {
	private static final long serialVersionUID = 1L;
	private AreaAtuacaoDAO areaAtuacaoDAO = AreaAtuacaoDAO.getInstance();
	private AreaAtuacao areaAtuacao = new AreaAtuacao();
	private boolean emEdicao = false;
	
	public AreaAtuacaoDAO getAreaAtuacaoDAO() {
		return this.areaAtuacaoDAO;
	}
	public void setAreaAtuacaoDAO(AreaAtuacaoDAO areaAtuacaoDAO) {
		this.areaAtuacaoDAO = areaAtuacaoDAO;
	}
	public AreaAtuacao getAreaAtuacao() {
		return this.areaAtuacao;
	}
	public void setAreaAtuacao(AreaAtuacao areaAtuacao) throws Exception {
		this.areaAtuacao = areaAtuacao;
	}
	public String novo()	{
		this.areaAtuacao = new AreaAtuacao();
		this.emEdicao = true;
		return "areaAtuacaoManter";
	}
	
	public String salvar(){
		this.areaAtuacaoDAO.salvar(this.areaAtuacao);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Dados salvos com sucesso!",""));
		this.emEdicao = false;
		return "areaAtuacaoManter";
	}
	
	public void editar(ActionEvent event) throws Exception {
		UIComponent link = event.getComponent();
		UIParameter param = (UIParameter) link.findComponent("editarAreaAtuacaoParam");
		this.areaAtuacao = (AreaAtuacao)param.getValue();
		this.emEdicao = true;
	}
	
	public void excluir(ActionEvent event)	{
	    UIComponent link = event.getComponent();
		UIParameter param = (UIParameter) link.findComponent("excluirAreaAtuacaoParam");
		Integer id = (Integer) param.getValue();
		this.areaAtuacaoDAO.remover(id);
		this.areaAtuacao = new AreaAtuacao();
		this.emEdicao = false;
	}
	
	public List<AreaAtuacao> getAreasAtuacao()	{
		List<AreaAtuacao> itens = this.areaAtuacaoDAO.listar();
		return itens;
	}
	
	public boolean getEmEdicao() {
		return this.emEdicao;
	}
	public void setEmEdicao(boolean emEdicao) {
		this.emEdicao = emEdicao;
	}
	public boolean getEmNavegacao() {
		return !this.emEdicao;
	}
}
